home *** CD-ROM | disk | FTP | other *** search
/ Almathera Ten Pack 3: CDPD 3 / Almathera Ten on Ten - Disc 3: CDPD3.iso / fish / 726-750 / 737 / dbuff / dbuff.c < prev    next >
C/C++ Source or Header  |  1995-03-18  |  13KB  |  455 lines

  1. /* #define DEMO 1 */
  2.  
  3. /***************************************************************************
  4.  
  5.    Program:    
  6.    File:       DBuff.c
  7.    
  8.    Version:    V1.3
  9.    Date:       05.02.91
  10.    Function:   Setup routines for double buffering based on Intuition screens
  11.    
  12.    Copyright:  SciTech Software 1991
  13.    Author:     Andrew C. R. Martin
  14.    Address:    SciTech Software
  15.                23, Stag Leys,
  16.                Ashtead,
  17.                Surrey,
  18.                KT21 2TD.
  19.    Phone:      +44 (0372) 275775
  20.    EMail:      UUCP: cbmuk!cbmuka!scitec!amartin
  21.                JANET: andrew@uk.ac.ox.biop
  22.                
  23. ****************************************************************************
  24.  
  25.    This program is not in the public domain, but it may be freely copied
  26.    and distributed for no charge providing this header is included.
  27.    The code may be modified as required, but any modifications must be
  28.    documented so that the person responsible can be identified. If someone
  29.    else breaks this code, I don't want to be blamed for code that does not
  30.    work! The code may not be sold commercially without prior permission from
  31.    the author, although it may be given away free with commercial products,
  32.    providing it is made clear that this program is free and that the source
  33.    code is provided with the program.
  34.  
  35. ****************************************************************************
  36.  
  37.    Description:
  38.    ============
  39.  
  40.    These routines set up, manipulate and tidy up for double buffered
  41.    animation using an Intuition Screen and Window
  42.  
  43. ****************************************************************************
  44.  
  45.    Usage:
  46.    ======
  47.  
  48.    5 routines are supplied:
  49.  
  50. >  RastPort = (struct RastPort *)InitDBuff(GfxBase,Screen,depth,Window)
  51.    --------------------------------------------------------------------
  52.    Given the GfxBase, Screen and Window, this routine returns the
  53.    second RastPort
  54.  
  55. >  SetView1()
  56.    ----------
  57. >  SetView2()
  58.    ----------
  59.    Sets the required view ready for drawing. SetView1() sets the Intuition
  60.    view, SetView2() sets the alternate view.
  61.    
  62. >  SwapView(Screen)
  63.    ----------
  64.    Displays the new view (after drawing)
  65.    
  66. >  FreeDBuff(screen,depth,RastPort)
  67.    --------------------------------
  68.    Frees up the memory, etc. for the second view.
  69.  
  70.    Notes:
  71.    ======
  72.    1. Assumes both graphics and intuition libraries are open
  73.    2. The window should be NOBORDER and have a null title
  74.    3. #define DEMO to see the demonstration.
  75.  
  76. ****************************************************************************
  77.  
  78.    Revision History:
  79.    =================
  80.  
  81.    V1.1  11.02.91
  82.    Fixed SwapView()
  83.  
  84.    V1.2  16.12.91
  85.    Tidied up and ANSIfied. Made globals static.
  86.    
  87.    V1.3  09.05.92
  88.    Fixed example (SetView()'s were the wrong way round!). Removed various
  89.    junk which didn't actually do anything!
  90.  
  91. ***************************************************************************/
  92. /* Includes
  93. */
  94.  
  95. #include <exec/types.h>
  96. #include <exec/memory.h>
  97. #include <intuition/intuition.h>
  98. #include <graphics/display.h>
  99. #include <graphics/gfxbase.h>
  100.  
  101. /**************************************************************************/
  102. /* Global Amiga system variables
  103. */
  104.  
  105. static struct RasInfo *DB_rinfo = NULL; /* Intuition supplied rasinfo     */
  106. static struct BitMap  *DB_bmap1 = NULL, /* Intuition supplied bitmap      */
  107.                       *DB_bmap2 = NULL; /* Second bitmap                  */
  108. static struct View    *DB_view  = NULL; /* Intuition View                 */
  109.  
  110. /**************************************************************************/
  111. InitDBuff(struct GfxBase *GfxBase,
  112.           struct Screen  *screen,
  113.           int            depth,
  114.           struct Window  *window)
  115. {
  116.    /* Amiga system variables */
  117.    struct RastPort *rport1    = NULL;  /* Intuition supplied rastport     */
  118.    struct ViewPort *vport     = NULL;  /* Intuition Viewport              */
  119.    struct RastPort *rport2    = NULL;  /* new rastport                    */
  120.    
  121.    int    j,
  122.           problem = 0;
  123.  
  124.    rport2 = NULL;
  125.  
  126.    /* Get the bitmap, rastport, viewport, view and rasinfo supplied by 
  127.       Intuition from the window, screen and graphics base from the call.
  128.    */
  129.    rport1      = window->RPort;
  130.    DB_bmap1    = rport1->BitMap;
  131.    vport       = &(screen->ViewPort);
  132.    DB_view     = GfxBase->ActiView;
  133.    DB_rinfo    = (*vport).RasInfo;
  134.  
  135.  
  136.    /*** Allocate second bitmap & memory for bitplanes ***/
  137.    if(!(DB_bmap2=(struct BitMap *)
  138.                  AllocMem(sizeof(struct BitMap), MEMF_PUBLIC|MEMF_CLEAR)))
  139.    {
  140.       printf("alloc bitmap failed\n");
  141.       rport2   = NULL;
  142.       problem  = 1;
  143.       goto EXITING;
  144.    }
  145.    InitBitMap(DB_bmap2, depth, screen->Width, screen->Height);
  146.    /* We'll use depth planes. */
  147.    for(j=0; j<depth; j++)
  148.    {
  149.       if (!(DB_bmap2->Planes[j] =
  150.          (PLANEPTR) AllocRaster(screen->Width, screen->Height)))
  151.       {
  152.          printf("alloc raster failed\n");
  153.          rport2   = NULL;
  154.          problem  = 1;
  155.          goto EXITING;
  156.       }
  157.    }
  158.  
  159.    /*** Create a rastport for this bitmap to simplify drawing ***/
  160.    if(!(rport2 = (struct RastPort *)
  161.                  AllocMem(sizeof(struct RastPort), MEMF_PUBLIC)))
  162.    {
  163.       printf("alloc rastport failed\n");
  164.       rport2   = NULL;
  165.       problem  = 1;
  166.       goto EXITING;
  167.    }
  168.    InitRastPort(rport2);
  169.    rport2->BitMap = DB_bmap2;
  170.  
  171.    SetRast(rport2, 0);
  172.  
  173.    /*** Swap views back and forth to get the MakeScreen()
  174.         out of the way    
  175.    ***/
  176.    SetView2();
  177.    MakeScreen(screen);
  178.    MrgCop(DB_view);
  179.  
  180.    SetView1();
  181.    MrgCop(DB_view);
  182.    
  183. EXITING:
  184.    if(problem) FreeDBuff(screen,depth,rport2);
  185.    
  186.    return((int)rport2);
  187. }
  188.  
  189. /**************************************************************************/
  190. SetView1(void)
  191. {
  192.    DB_rinfo->BitMap  = DB_bmap1;
  193.    return(0);
  194. }
  195.  
  196. /**************************************************************************/
  197. SetView2(void)
  198. {
  199.    DB_rinfo->BitMap  = DB_bmap2;
  200.    
  201.    return(0);
  202. }
  203.  
  204. /**************************************************************************/
  205. SwapView(struct Screen *scrn)
  206. {
  207.    MakeScreen(scrn);
  208.    MrgCop(DB_view);
  209.    
  210.    return(0);
  211. }
  212.  
  213. /**************************************************************************/
  214. FreeDBuff(struct Screen    *screen,
  215.           int              depth,
  216.           struct RastPort  *rport2)
  217. {
  218.    int j;
  219.    
  220.    /* Ensure we're on the Intuition-supplied bit map before exiting */
  221.    SetView1();
  222.    MrgCop(DB_view);
  223.  
  224.    /* Free up the second rastport */
  225.    if(rport2) FreeMem(rport2, sizeof(struct RastPort));
  226.  
  227.    /* Free up the second bit map */
  228.    if(DB_bmap2) 
  229.    {
  230.       for(j=0;j<depth;j++)
  231.       {
  232.          if(DB_bmap2->Planes[j])
  233.             FreeRaster(DB_bmap2->Planes[j], screen->Width, screen->Height);
  234.       }
  235.       FreeMem(DB_bmap2, sizeof (struct BitMap));
  236.    }
  237.    
  238.    return(0);
  239. }
  240.  
  241. /***************************************************************************
  242. *                                                                          *
  243. *              DEMO CODE --- Compiled if DEMO defined                      *
  244. *                                                                          *
  245. ***************************************************************************/
  246. #ifdef DEMO
  247.  
  248. #include <exec/types.h>
  249. #include <intuition/intuition.h>
  250. #include <graphics/display.h>
  251.  
  252. #define DEPTH 1      /* Screen depth (number of bitplanes)                */
  253.  
  254. /* Global Amiga system variables */
  255. struct  IntuitionBase   *IntuitionBase = NULL;
  256. struct  GfxBase         *GfxBase       = NULL;
  257.  
  258. main()
  259. {
  260.    /* Amiga system variables */
  261.    struct Screen   *screen    = NULL;  /* Intuition screen                */
  262.    struct Window   *window    = NULL;  /* Intuition window                */
  263.    struct RastPort *rport1    = NULL,  /* Intuition supplied rastport     */
  264.                    *rport2    = NULL;
  265.  
  266.    int    j,
  267.           exitval = 0;
  268.  
  269.  
  270.    /*** Open Libraries ***/
  271.    if(!(IntuitionBase = (struct IntuitionBase *)
  272.                         OpenLibrary("intuition.library", 0)))
  273.    {
  274.       printf("NO INTUITION LIBRARY\n");
  275.       exitval = 1;
  276.       goto CRAPOUT;
  277.    }
  278.  
  279.    if(!(GfxBase = (struct GfxBase *)
  280.                   OpenLibrary("graphics.library", 0)))
  281.    {
  282.       printf("NO GRAPHICS LIBRARY\n");
  283.       exitval = 2;
  284.       goto CRAPOUT;
  285.    }
  286.  
  287.    /*** Open Intuition screen and window ***/
  288.    if((screen = (struct Screen *)make_screen(0,640,400,DEPTH,-1,-1,
  289.                 HIRES|INTERLACE,"***Test Screen***"))==NULL)
  290.    {
  291.       printf("Can't open screen\n");
  292.       exitval = 1;
  293.       goto CRAPOUT;
  294.    }
  295.  
  296.    /*** Open a window on the new screen ***/
  297.    if((window = (struct Window *)make_window(0,0,640,400,NULL,
  298.                 BORDERLESS|SIMPLE_REFRESH,NULL,-1,-1,screen,NULL))==NULL)
  299.    {
  300.       printf("Can't open window\n");
  301.       exitval = 1;
  302.       goto CRAPOUT;
  303.    }
  304.  
  305.    rport1 = window->RPort;
  306.    rport2 = (struct RastPort *)InitDBuff(GfxBase,screen,DEPTH,window);
  307.  
  308.    /*** Loop which draws into alternate bitmaps and swaps view ***/
  309.    for(j=0;j<500;j++)
  310.    {
  311.       if(j%2)  /* Odd cases --- Draw to rport1 */
  312.       {
  313.          SetView1();
  314.  
  315.          if(j-2 > 0)  DrawFigure(rport1,j-2,0); /* Clear previous version */
  316.          DrawFigure(rport1,j,1);                /* Draw this version      */
  317.       }
  318.       else     /* Even cases --- Draw to rport2 */
  319.       {
  320.          SetView2();
  321.  
  322.          if(j-2 >= 0) DrawFigure(rport2,j-2,0); /* Clear previous version */
  323.          DrawFigure(rport2,j,1);                /* Draw this version      */
  324.       }
  325.       
  326.       SwapView(screen);
  327.       Delay(1);
  328.    }
  329.    
  330.    
  331.  
  332. CRAPOUT:
  333.    FreeDBuff(screen,DEPTH,rport2);
  334.    
  335.    /* Close stuff */
  336.    if(window)        CloseWindow(window);
  337.    if(screen)        CloseScreen(screen);
  338.    if(GfxBase)       CloseLibrary(GfxBase);
  339.    if(IntuitionBase) CloseLibrary(IntuitionBase);
  340.  
  341.    exit(exitval);
  342. }
  343.  
  344. /**************************************************************************/
  345. make_screen(SHORT    y,
  346.             SHORT    w,
  347.             SHORT    h,
  348.             SHORT    d,
  349.             UBYTE    colour0,
  350.             UBYTE    colour1,
  351.             USHORT   mode,
  352.             UBYTE    *name)
  353. {
  354.    struct NewScreen NewScreen;
  355.  
  356.  
  357.    NewScreen.LeftEdge      = 0;
  358.    NewScreen.TopEdge       = y;
  359.    NewScreen.Width         = w;
  360.    NewScreen.Height        = h;
  361.    NewScreen.Depth         = d;
  362.    NewScreen.DetailPen     = colour0;
  363.    NewScreen.BlockPen      = colour1;
  364.    NewScreen.ViewModes     = mode;
  365.    NewScreen.Type          = CUSTOMSCREEN;
  366.    NewScreen.Font          = NULL;
  367.    NewScreen.DefaultTitle  = name;
  368.    NewScreen.Gadgets       = NULL;
  369.    NewScreen.CustomBitMap  = NULL;
  370.  
  371.    return(OpenScreen(&NewScreen));
  372. }
  373.  
  374. /**************************************************************************/
  375. /* Initialize a NewWindow and Open it.
  376. */
  377. make_window(SHORT         x,
  378.             SHORT         y,
  379.             SHORT         w,
  380.             SHORT         h,
  381.             UBYTE         *name, 
  382.             ULONG         flags,
  383.             ULONG         iflags,
  384.             UBYTE         colour0,
  385.             UBYTE         colour1,
  386.             struct Screen *screen,
  387.             struct Gadget *gadg)
  388. {
  389.    struct NewWindow NewWindow;
  390.  
  391.    NewWindow.LeftEdge      = x;
  392.    NewWindow.TopEdge       = y;
  393.    NewWindow.Width         = w;
  394.    NewWindow.Height        = h ;
  395.    NewWindow.DetailPen     = colour0;
  396.    NewWindow.BlockPen      = colour1;
  397.    NewWindow.Title         = name;
  398.    NewWindow.Flags         = flags;
  399.    NewWindow.IDCMPFlags    = iflags;
  400.    NewWindow.Type          = CUSTOMSCREEN;
  401.    NewWindow.FirstGadget   = gadg;
  402.    NewWindow.CheckMark     = NULL;
  403.    NewWindow.Screen        = screen;
  404.    NewWindow.BitMap        = NULL;
  405.    NewWindow.MinWidth      = 0;
  406.    NewWindow.MinHeight     = 0;
  407.    NewWindow.MaxWidth      = 0;
  408.    NewWindow.MaxHeight     = 0;
  409.  
  410.    return(OpenWindow(&NewWindow));
  411. }
  412.  
  413. /**************************************************************************/
  414. DrawFigure(struct RastPort *rp,
  415.            int             n,
  416.            int             p)
  417. {
  418.    SetAPen(rp,p);
  419.    Move(rp,n+20,270);
  420.    Draw(rp,n+0,250);
  421.    Draw(rp,n+0,220);
  422.    Draw(rp,n+20,200);
  423.    Draw(rp,n+40,220);
  424.    Draw(rp,n+40,250);
  425.    Draw(rp,n+20,270);
  426.    Draw(rp,n+20,300);
  427.    Draw(rp,n+40,320);
  428.    Draw(rp,n+20,340);
  429.    Move(rp,n+40,320);
  430.    Draw(rp,n+70,320);
  431.    Draw(rp,n+70,340);
  432.    Move(rp,n+70,320);
  433.    Draw(rp,n+90,300);
  434.  
  435.    Move(rp,n+20, 70);
  436.    Draw(rp,n+0,  50);
  437.    Draw(rp,n+0,  20);
  438.    Draw(rp,n+20, 00);
  439.    Draw(rp,n+40, 20);
  440.    Draw(rp,n+40, 50);
  441.    Draw(rp,n+20, 70);
  442.    Draw(rp,n+20,100);
  443.    Draw(rp,n+40,120);
  444.    Draw(rp,n+20,140);
  445.    Move(rp,n+40,120);
  446.    Draw(rp,n+70,120);
  447.    Draw(rp,n+70,140);
  448.    Move(rp,n+70,120);
  449.    Draw(rp,n+90,100);
  450.  
  451.    return(0);
  452. }
  453.                
  454. #endif /* DEMO */
  455.